package BVRServer;

import org.apache.mina.common.IoSession;

import BVRClient.BVRC_Camera;
import BVRClient.BVRC_Entity;
import BVRClient.BVRC_Map;
import BVRClient.BVRC_Point;
import BVRClient.BVRC_Rect;
import BVRClient.BVRC_Player.PLAYER_TEAM;

public class BVRS_Player extends BVRC_Entity
{
	private String name = "";
	
	private int movementSpeedX = 0;
	private int movementSpeedY = 0;
	
	private int playerID = -1;
	
	private PLAYER_TEAM team = PLAYER_TEAM.RED;
	
	private BVRC_Rect tempRect = new BVRC_Rect(0, 0, 0, 0);
	
	private float rotation = 0;
	
	private IoSession session = null;
	
	/**
	 * 
	 */
	public BVRS_Player() {}
	
	/**
	 * 
	 * @param delta
	 * @return true if the movement was successful, false in all other cases
	 */
	public boolean updateMovement(int index, int delta, BVRC_Map currentMap)
	{
		if (this.movementSpeedX == 0 && this.movementSpeedY == 0)
			return false;
		
		this.tempRect.x = this.entityRect.x;
		this.tempRect.y = this.entityRect.y;
		
		float velocityX = (float)this.movementSpeedX * (float)(delta / 1000.0f);
		float velocityY = (float)this.movementSpeedY * (float)(delta / 1000.0f);
		
		this.entityRect.x += velocityX;
		this.entityRect.y -= velocityY;
		
		if (currentMap.rectMapCollision(this.entityRect))
		{
			this.entityRect.x = this.tempRect.x;
			this.entityRect.y = this.tempRect.y;
			
			return false;
		}
		
		return true;
	}
	
	/**
	 * 
	 * @param p
	 * @param camera
	 */
	public void rotateFacePoint(BVRC_Point p, BVRC_Camera camera)
	{
		double alpha = Math.atan2(p.y - (this.entityRect.y - camera.getVisibleRect().y), p.x - (this.entityRect.x - camera.getVisibleRect().x));
		
		rotation = (float) (alpha * 180 / Math.PI) + 90;
	}
	
	/**
	 * 
	 * @param name
	 */
	public void setName(String name)
	{
		this.name = name;
	}
	
	/**
	 * 
	 * @return the player's name
	 */
	public String getName()
	{
		return this.name;
	}
	
	/**
	 * 
	 * @param team
	 */
	public void setTeam(PLAYER_TEAM team)
	{
		this.team = team;
	}
	
	/**
	 * 
	 * @return the player's team
	 */
	public PLAYER_TEAM getTeam()
	{
		return this.team;
	}
	
	/**
	 * 
	 * @param movementSpeedX
	 */
	public void setMovementSpeedX(int movementSpeedX)
	{
		this.movementSpeedX = movementSpeedX;
	}
	
	/**
	 * 
	 * @return the player's current X axis movement speed
	 */
	public int getMovementSpeedX()
	{
		return this.movementSpeedX;
	}
	
	/**
	 * 
	 * @param movementSpeedY
	 */
	public void setMovementSpeedY(int movementSpeedY)
	{
		this.movementSpeedY = movementSpeedY;
	}
	
	/**
	 * 
	 * @return the player's current Y axis movement speed
	 */
	public int getMovementSpeedY()
	{
		return this.movementSpeedY;
	}
	
	/**
	 * 
	 * @param rotation
	 */
	public void setRotation(float rotation)
	{
		this.rotation = rotation;
	}
	
	/**
	 * 
	 * @return the player's rotation angle
	 */
	public float getRotation()
	{
		return this.rotation;
	}
	
	/**
	 * 
	 * @param playerID
	 */
	public void setPlayerID(int playerID)
	{
		this.playerID = playerID;
	}
	
	/**
	 * 
	 * @return the player's current ID
	 */
	public int getPlayerID()
	{
		return playerID;
	}
	
	/**
	 * 
	 * @param session
	 */
	public void setSession(IoSession session)
	{
		this.session = session;
	}
	
	/**
	 * 
	 * @return the player's network session
	 */
	public IoSession getSession()
	{
		return this.session;
	}
}
